package com.sf.dlb.web.controller;

import com.sf.dlb.bean.UserMessageBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.service.UserMessageService;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import com.sf.dlb.web.config.Global;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 用户消息 相关
 * Created by jing.tan on 2018/12/10.
 */
@RestController
@RequestMapping(value = "/v1/userMessage")
public class UserMessageController {
    private static Logger logger = LoggerFactory.getLogger(AppVersionController.class);
    @Resource
    private Global global;
    @Autowired
    private UserMessageService userMessageService;
    /**
     * 消息列表
     *
     * @return
     */
    @GetMapping(value = {"/list/{type}","/list"})
    @ResponseBody
    public Object list(HttpServletRequest request,@PathVariable(required = false) String type) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        resultVO=userMessageService.list(type);
        logger.info("list response json " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 增加用户消息
     */
    @PostMapping(value = "/add")
    @ResponseBody
    public Object add(HttpServletRequest request,@RequestBody UserMessageBean userMessageBean) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        logger.info("addAppVersion request json === " + GsonUtils.getInstance().toJson(userMessageBean));
        if(StringUtils.isEmpty(userMessageBean.getType())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "消息类型不能为空");
        }
        if(StringUtils.isEmpty(userMessageBean.getTitle())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "消息标题不能为空");
        }
        if(StringUtils.isEmpty(userMessageBean.getContent())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "消息内容不能为空");
        }
        resultVO=userMessageService.add(userMessageBean);
        logger.info("add response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 更新用户消息
     */
    @PostMapping(value = "/update")
    @ResponseBody
    public Object update(HttpServletRequest request, @RequestBody UserMessageBean userMessageBean) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        logger.info("update request json === " + GsonUtils.getInstance().toJson(userMessageBean));
        if(StringUtils.isEmpty(userMessageBean.getId())){
            return ResultVOUtil.error(ResponseConstant.ERROR, "缺少id");
        }
        resultVO=userMessageService.update(userMessageBean);
        logger.info("update response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
}
